package org.jash.sportnews2109.ui

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import org.jash.common.logging
import org.jash.common.mvvm.BaseActivity
import org.jash.common.token
import org.jash.sportnews2109.R
import org.jash.sportnews2109.BR
import org.jash.sportnews2109.databinding.ActivityLoginBinding
import org.jash.sportnews2109.vm.LoginViewModel

@Route(path = "/news/login")
class LoginActivity : BaseActivity<ActivityLoginBinding, LoginViewModel>() {
    val user by lazy { mutableMapOf<String, String>("name" to "qqqq", "password" to "1111") }
    override fun initView() {

        binding.user = user
        binding.login.setOnClickListener {
            viewModel.login(user)
        }
        binding.phone.setOnClickListener {
            ARouter.getInstance().build("/news/phone")
                .navigation()
        }
        binding.register.setOnClickListener {
            ARouter.getInstance().build("/news/register")
                .navigation()
        }
        setSupportActionBar(binding.toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
    }

    override fun initData() {
        viewModel.tokenLiveData.observe(this, this::success)
    }

    fun success(t:String) {
        token = t
        Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show()
        logging("token: $token")
        finish()

    }
}